package com.rocksea.app.db.HighStrain;

import com.alibaba.fastjson.JSON;
import com.litesuits.orm.db.annotation.Default;
import com.litesuits.orm.db.annotation.Ignore;
import com.litesuits.orm.db.annotation.Mapping;
import com.litesuits.orm.db.annotation.PrimaryKey;
import com.litesuits.orm.db.annotation.Table;
import com.litesuits.orm.db.enums.AssignType;
import com.litesuits.orm.db.enums.Relation;
import com.mor.dataacquisition.vo.highStrain.*;
import com.mor.dataacquisition.vo.highStrain.ChannelData;
import com.mor.dataacquisition.vo.highStrain.HammerData;
import com.rocksea.app.Account;
import com.rocksea.app.entry.HighStarinDb;
import com.rocksea.app.http.HttpUtil;
import com.rocksea.app.utils.PerUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Mrw on 2017/8/5.
 *
 */
@Table("HighStrain")
public class HighStrain {
    @PrimaryKey(AssignType.AUTO_INCREMENT)
    private int id;
    @Ignore
    public String randomCode;
    @Ignore
    public String account;

    public Long inspectionFormId;

    public String inspectionFormNum;

    @Default("0")
    public int upLoadTime;
    @Default("0")
    public byte isLoad;

    @Default("0")
    public long revicedTime;

    public String uuid;
    public String vendorId;
    public String machineId;
    public String serialNo;
    public long pileId;
    public String pileNo;
    public String fileName;
    public String startTime;
    public Integer totalHammers;
    public int fileType;
    public String coordinateX;
    public String coordinateY;
    public int GpsIsValid;
    public String sourceData;

    @Ignore
    public List<com.rocksea.app.db.HighStrain.HammerData>  hdList;

    public HighStrain()
    {

    }

    public HighStrain(String json)
    {
        HighStarinDb mHighStrain = JSON.parseObject(json,HighStarinDb.class);
        uuid = mHighStrain.BaseInfoId;
        vendorId = mHighStrain.VendorID;
        machineId = mHighStrain.MachineID;
        serialNo = mHighStrain.SerialNo;
        pileNo = mHighStrain.PileNo;
        fileName = mHighStrain.FileName;
        startTime = mHighStrain.StartTime;
        totalHammers = mHighStrain.TotalHammers;
        fileType = mHighStrain.FileType;
        revicedTime = new Date().getTime();
        coordinateX = HttpUtil.encyptDES(mHighStrain.CoordinateX + "");
        coordinateY = HttpUtil.encyptDES(mHighStrain.CoordinateY + "");
        GpsIsValid = 1;
        sourceData = json;

        hdList = new ArrayList<>();
        // <editor-fold desc="高应变 ">
        for (HighStarinDb.HammerDataBean hammerDatum : mHighStrain.HammerData)
        {
            com.rocksea.app.db.HighStrain.HammerData hammerData = new com.rocksea.app.db.HighStrain.HammerData();
            hammerData. curHammer = hammerDatum.CurHammer;
            hammerData. pileDiameter = hammerDatum.PileDiameter;
            hammerData. pileLength = hammerDatum.PileLength;
            hammerData. lengthUnderSensor = hammerDatum.LengthUnderSensor;
            hammerData. channelNumber = hammerDatum.ChannelNumber;
            hammerData. lengthInSoil = hammerDatum.LengthInSoil;
            hammerData. TestArea = hammerDatum.TestArea;
            hammerData. bottomArea = hammerDatum.BottomArea;
            hammerData. pileArea = hammerDatum.PileArea;
            hammerData. pileDensity = hammerDatum.PileDensity;
            hammerData. testDensity = hammerDatum.TestDensity;
            hammerData. avgSpeed = hammerDatum.AvgSpeed;
            hammerData. testSpeed = hammerDatum.TestSpeed;
            hammerData. Js = hammerDatum.Js;
            hammerData. hammerDropheight = hammerDatum.HammerDropHeight;
            hammerData. hammerWeight = hammerDatum.HammerWeight;
            hammerData. samplePoints = hammerDatum.SamplePoints;
            hammerData. sampleInterval = hammerDatum.SampleInterval;
            hammerData. powerBearing = hammerDatum.PowerBearing;
            hammerData. depthIn = hammerDatum.DepthIn;
            hammerData. preDepthPerHammer = hammerDatum.PreDepthPerHammer;
            hammerData. method = hammerDatum.Method;
            hammerData. isOperateCH1 = hammerDatum.IsOperateCH1;
            hammerData. isOperateCH2 = hammerDatum.IsOperateCH2;
            hammerData. isOperateCH3 = hammerDatum.IsOperateCH3;
            hammerData. isOperateCH4 = hammerDatum.IsOperateCH4;
            hammerData. RSP = hammerDatum.RSP;
            hammerData. RMX = hammerDatum.RMX;
            hammerData. RSU = hammerDatum.RSU;
            hammerData. FMX = hammerDatum.FMX;
            hammerData. VMX = hammerDatum.VMX;
            hammerData. EMX = hammerDatum.EMX;
            hammerData. DMX = hammerDatum.DMX;
            hammerData. DFN = hammerDatum.DFN;
            hammerData. CSX = hammerDatum.CSX;
            hammerData. TSX = hammerDatum.TSX;
            hammerData. cileTop = hammerDatum.PileTop;
            hammerData. cileBottom = hammerDatum.PileBottom;
            hammerData. cectionCircum = hammerDatum.SectionCircum;
            hammerData. concreteStrength = hammerDatum.ConcreteStrength;
            hammerData. customParam = hammerDatum.CustomParam;
            hammerData.cdList = new ArrayList<>();
            for (HighStarinDb.ChannelDataBean channelDatum : hammerDatum.ChannelData)
            {
                com.rocksea.app.db.HighStrain.ChannelData channelData = new com.rocksea.app.db.HighStrain.ChannelData();
                channelData.channelNo = channelDatum.ChannelNo;
                channelData.chnGain = channelDatum.ChnGain;
                channelData.delayPoints = channelDatum.DelayPoints;
                channelData.sensorCH = (int) channelDatum.SensorCH;
                channelData.uuid = channelDatum.uuid;
                channelData.baseInfoId = channelDatum.BaseInfoId;
                channelData.sensorType = channelDatum.SensorType;
                channelData.waveData = channelDatum.WaveData;
                hammerData.cdList.add(channelData);
            }
            hdList.add(hammerData);
        }
        // </editor-fold>
    }

    public com.mor.dataacquisition.vo.highStrain.HighStrain getBJHighStrain(long inspectionFormId,String inspectionFormNum,long pileId)
    {
        com.mor.dataacquisition.vo.highStrain.HighStrain highStrain = new com.mor.dataacquisition.vo.highStrain.HighStrain();

        highStrain.account = Account.getAccount();
        highStrain.randomCode = Account.getRandomCode();
        highStrain.inspectionFormId = inspectionFormId;
        highStrain.inspectionFormNum =  inspectionFormNum;
        highStrain.pileId = pileId;
        highStrain.coordinateX = coordinateX;
        highStrain.coordinateY = coordinateY;
        highStrain.fileName = fileName;
        highStrain.machineId = machineId;
        highStrain.serialNo = "RSGY";
        highStrain.testTime = startTime;
        highStrain.totalHammers = totalHammers;
        highStrain.fileType = fileType;
        highStrain.pileNo = pileNo;
        highStrain.vendorId = vendorId;
        highStrain.hdList = new ArrayList<>();

        for (int i = 0; i < hdList.size(); i++)
        {
            com.mor.dataacquisition.vo.highStrain.HammerData hammerData = new HammerData();
            com.rocksea.app.db.HighStrain.HammerData hammerDataBean = hdList.get(i);

            if (hammerDataBean.curHammer > 9)
                hammerData.curHammer = 9;
            else
                hammerData.curHammer = hammerDataBean.curHammer;
            hammerData.pileDiameter = (int) hammerDataBean.pileDiameter;
            hammerData.pileLength = PerUtils.getTwoData(hammerDataBean.pileLength);
            hammerData.lengthUnderSensor = PerUtils.getTwoData(hammerDataBean.lengthUnderSensor);
            hammerData.channelNumber = hammerDataBean.channelNumber;
            hammerData.lengthInSoil = PerUtils.getTwoData(hammerDataBean.lengthInSoil);
            hammerData.testArea = PerUtils.getTwoData(hammerDataBean.TestArea);
            hammerData.bottomArea = PerUtils.getTwoData(hammerDataBean.bottomArea);
            hammerData.pileArea = PerUtils.getTwoData(hammerDataBean.pileArea);
            hammerData.pileDensity = PerUtils.getTwoData(hammerDataBean.pileDensity);
            hammerData.testDensity = PerUtils.getTwoData(hammerDataBean.testDensity);

            if ((int) hammerDataBean.avgSpeed > 9999)
                hammerData.avgSpeed = 9999;
            else
                hammerData.avgSpeed = (int) hammerDataBean.avgSpeed;
            if ((int) hammerDataBean.testSpeed < 9999)
                hammerData.testSpeed = (int) hammerDataBean.testSpeed;
            else
                hammerData.testSpeed = 9999;

            hammerData.Js = PerUtils.getTwoData(hammerDataBean.Js);
            if ((int) hammerDataBean.hammerDropheight * 1000 < 9999)
                hammerData.hammerDropheight = (int) hammerDataBean.hammerDropheight * 1000;
            else
                hammerData.hammerWeight = 9999;

            hammerData.samplePoints = hammerDataBean.samplePoints;
            if (hammerDataBean.sampleInterval < 9999)
                hammerData.sampleInterval = hammerDataBean.sampleInterval;
            else
                hammerData.sampleInterval = 9999;

            hammerData.powerBearing = PerUtils.getTwoData(hammerDataBean.powerBearing);

            hammerData.depthIn = PerUtils.getTwoData(hammerDataBean.depthIn);

            hammerData.preDepthPerHammer = PerUtils.getTwoData(hammerDataBean.preDepthPerHammer);

            hammerData.RSP = PerUtils.getTwoData(hammerDataBean.RSP);
            hammerData.RMX = PerUtils.getTwoData(hammerDataBean.RMX);
            hammerData.RSU = PerUtils.getTwoData(hammerDataBean.RSU);
            hammerData.FMX = PerUtils.getTwoData(hammerDataBean.FMX);

            hammerData.VMX = (int) hammerDataBean.VMX;
            hammerData.EMX = (int) hammerDataBean.EMX;
            hammerData.DMX = (int) hammerDataBean.DMX;
            hammerData.DFN = (int) hammerDataBean.DFN;

            hammerData.CSX = PerUtils.getTwoData(hammerDataBean.CSX);
            hammerData.TSX = PerUtils.getTwoData(hammerDataBean.TSX);
            hammerData.cileTop = hammerDataBean.cileTop;
            hammerData.cileBottom = hammerDataBean.cileBottom;
            hammerData.cectionCircum = PerUtils.getTwoData(hammerDataBean.cectionCircum);
            hammerData.concreteStrength = hammerDataBean.concreteStrength;
            hammerData.customParam = new JSONObject().toString();
            hammerData.cdList = new ArrayList<>();

            for (int m = 0; m < hammerDataBean.cdList.size(); m++)
            {
                com.rocksea.app.db.HighStrain.ChannelData channelDatu = hammerDataBean.cdList.get(m);
                ChannelData channelData = new ChannelData();
                channelData.uuid= channelDatu.uuid;
                channelData.channelNo= channelDatu.channelNo;
                channelData.sensorType= channelDatu.sensorType;
                channelData.delayPoints= channelDatu.delayPoints;
                channelData.chnGain= channelDatu.chnGain;
                channelData.sensorCH= PerUtils.getTwoData(channelDatu.sensorCH);
                String waveData = channelDatu.waveData;
                String[] data = waveData.split(",");
                channelData.waveData = new float[data.length];
                for (int n = 0; n < data.length; n++)
                {
                    if (channelData.sensorType == 2)
                    {
                        channelData.waveData[n] = (float) PerUtils.getTwoData(Float.parseFloat(data[n]) / 1000);
                    }
                    else
                        channelData.waveData[n] = (float) PerUtils.getTwoData(Float.parseFloat(data[n]));

                }
                hammerData.cdList.add(channelData);
            }
            highStrain.hdList.add(hammerData);
        }
        return highStrain;
    }

}
